/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RealMachineFrame.java
 *
 * Created on 2012-03-10, 21.05.13
 */

package lt.vu.mif.os;

import java.io.File;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.table.*;
import lt.vu.mif.os.codereader.CodeReader;
import lt.vu.mif.os.memory.UserMemory;
import lt.vu.mif.os.processes.JobGovernor;
import lt.vu.mif.os.provider.Constants;
/**
* Real machine's GUI.
* 
* @author Antanas Rudaitis
* @author Laurynas Paradauskas
* @author Algirdas Šaltupis
* 
*/
public class RealMachineFrame extends javax.swing.JFrame {
    public boolean inputOn;
    public long inputStartTime;
    public long inputStopTime;
    /** Creates new form RealMachineFrame */
    public RealMachineFrame(java.awt.Frame parent, boolean modal) {
        initComponents();
        inputOn = false;
        processesModel = new DefaultTableModel();
        jTable1.setModel(processesModel);
        processesModel.addColumn("Process");
        processesModel.addColumn("State");
        setInput(false);
    }

    public void setInput(boolean value) {
    	jTextField1.setEnabled(value);
    }
    
    public String getFromInput() {
    	return jTextField1.getText();
    }
    
    public void setNextStep(boolean value) {
        jButton1.setEnabled(value);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        button1 = new java.awt.Button();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        ptrField = new javax.swing.JTextField();
        spField = new javax.swing.JTextField();
        pcField = new javax.swing.JTextField();
        tiField = new javax.swing.JTextField();
        modeField = new javax.swing.JTextField();
        siField = new javax.swing.JTextField();
        piField = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        userMem = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Real Machine");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        button1.setActionCommand("Load application");
        button1.setLabel("Load application");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Registers"));

        jLabel1.setText("PTR");

        jLabel2.setText("SP");

        jLabel3.setText("PC");

        jLabel4.setText("TI");

        jLabel5.setText("MODE");

        jLabel6.setText("SI");

        jLabel7.setText("PI");

        ptrField.setEditable(false);
        ptrField.setText("jTextField1");

        spField.setEditable(false);
        spField.setText("jTextField2");

        pcField.setEditable(false);
        pcField.setText("jTextField3");

        tiField.setEditable(false);
        tiField.setText("jTextField4");

        modeField.setEditable(false);
        modeField.setText("jTextField5");

        siField.setEditable(false);
        siField.setText("jTextField6");

        piField.setEditable(false);
        piField.setText("jTextField7");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                        .addComponent(ptrField, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                        .addComponent(spField, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(piField)
                            .addComponent(siField)
                            .addComponent(modeField)
                            .addComponent(tiField)
                            .addComponent(pcField, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(ptrField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(spField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(pcField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tiField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(modeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(siField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(piField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("User memory"));

        userMemModel = new DefaultTableModel();
        Object[] tmpValue = new Object[lt.vu.mif.os.provider.Constants.blockSize-1];

        userMemModel.addColumn("");
        for (int i=0; i<lt.vu.mif.os.provider.Constants.blockSize; i++) {
            userMemModel.addColumn(i);
        }

        int howManyLines = lt.vu.mif.os.provider.Constants.userMemSize/lt.vu.mif.os.provider.Constants.blockSize;

        for (int i=0; i<howManyLines; i++) {
            userMemModel.insertRow(userMemModel.getRowCount(), tmpValue);
            userMemModel.setValueAt(i, i, 0);
        }

        int rowCount = 0;
        int columnCount = 1;

        for (int i=0; i<lt.vu.mif.os.provider.Constants.userMemSize; i++) {
            if ((i%lt.vu.mif.os.provider.Constants.blockSize==0) && (i!=0)) {
                rowCount++;
                columnCount = 1;
            }
            userMemModel.setValueAt(lt.vu.mif.os.memory.UserMemory.getWord(i).get(), rowCount, columnCount);
            columnCount++;
        }
        userMem.setModel(userMemModel);
        jScrollPane1.setViewportView(userMem);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 704, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jLabel9.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText("Real  Machine");

        jButton1.setText("Next step");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Console"));

        jTextArea1.setBackground(new java.awt.Color(0, 0, 0));
        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setFont(new java.awt.Font("Monospaced", 1, 12)); // NOI18N
        jTextArea1.setForeground(new java.awt.Color(255, 255, 255));
        jTextArea1.setRows(5);
        jTextArea1.setDisabledTextColor(new java.awt.Color(102, 255, 0));
        jTextArea1.setVerifyInputWhenFocusTarget(false);
        jScrollPane3.setViewportView(jTextArea1);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Processes"));

        jScrollPane4.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );

        jLabel8.setText("VM started:");

        jLabel10.setText("jLabel10");

        jTextField1.setEnabled(false);
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(268, 268, 268)
                .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                .addGap(262, 262, 262))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jTextField1)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel10)
                                .addGap(28, 28, 28)
                                .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                                .addComponent(jButton1)
                                .addGap(149, 149, 149))
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(jLabel10)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jButton1))
                    .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
        JFileChooser jFileChooser1 = new JFileChooser();
        //final RealMachineFrame rm = this;
        int option = jFileChooser1.showOpenDialog(RealMachineFrame.this);
        if (option == JFileChooser.APPROVE_OPTION) {
          final File sf = jFileChooser1.getSelectedFile();
          final String f = sf.getPath();
          new CodeReader(f, RealMachine.getVMCounter());
          RealMachine.incVMCounter();
          RealMachine.addVm(new VirtualMachine(UserMemory.getMemory(), f, RealMachine.getVMCounter()));
          
        }
    }//GEN-LAST:event_button1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        /*List<VirtualMachine> vmList = RealMachine.getAllVm();
        System.out.println(vmList.size());
        for (VirtualMachine vm : vmList) {
            vm.step();
            updateRMGUI();
        }*/
    	if (inputOn) {
    		inputStopTime = System.currentTimeMillis();
    		inputOn = false;
    		
    		if (inputStopTime-inputStartTime <= 10000){
	    		List<VirtualMachine> vmList = RealMachine.getAllVm();
	    		
	    		VirtualMachine vm = null;
	    		
	    		for (VirtualMachine vm2 : vmList) {
	    			if (vm2.getState() == Constants.RUNNING) {
	    				vm = vm2;
	    				break;
	    			}
	    		}
	    		if (vm != null)
	    			vm.addToStack(jTextField1.getText());
    		}	
    		else {
    			System.out.println("ERROR: INPUT TIME EXCEETED!!!");
    		}
    		jTextField1.setText("");
    		this.setInput(false);
    		
    	}
        RealMachine.jobgov.work();
        this.updateRMGUI();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed

    }//GEN-LAST:event_jTextField1ActionPerformed

    public void updateRMMemoryState() {
      int rowCount = 0;
      int columnCount = 1;
      for (int i=0; i<lt.vu.mif.os.provider.Constants.userMemSize; i++) {
        if ((i%lt.vu.mif.os.provider.Constants.blockSize==0) && (i!=0)) {
          rowCount++;
          columnCount = 1;
        }
        userMemModel.setValueAt(lt.vu.mif.os.memory.UserMemory.getWord(i).get(), rowCount, columnCount);
        columnCount++;
      }
    }

    public void updatePTRField(String ptr) {
      ptrField.setText(ptr);
    }

    public void updateModeField(String mode) {
      modeField.setText(mode);
    }

    public void updatePCField(String pc) {
      pcField.setText(pc);
    }
    
    public void printToConsole(String string) {
        jTextArea1.setText(string+"\n"+jTextArea1.getText());
    }
    
    public void updateVMCounterField(int counter) {
        jLabel10.setText(Integer.toString(counter));
    }

    public void updatePIField(String pi) {
      piField.setText(pi);
    }

    public void updateSIField(String si) {
      siField.setText(si);
    }

    public void updateSPField(String sp) {
      spField.setText(sp);
    }

    public void updateTIField(String ti) {
      tiField.setText(ti);
    }
    
    public void updateRMGUI() {
    	this.updateRMMemoryState();
        updateProcess();
    }
    
    public void updateProcess() {
        processesModel.setRowCount(0);
        addNewProcess("JobGovernor", Constants.RUNNING);
        
        List<VirtualMachine> list = RealMachine.getAllVm();
        for (VirtualMachine vm : list) {
            addNewProcess(vm.toString(), vm.getState());
            System.out.println("VM state: "+vm.getState());
        }
    }
    
    public int addNewProcess(String name, int state) {
        Object[] tmp = new Object[2];
    
        processesModel.insertRow(processesModel.getRowCount(), tmp);
        
        processesModel.setValueAt(name, processesModel.getRowCount()-1, 0);
        processesModel.setValueAt(stateToString(state), processesModel.getRowCount()-1, 1);
        
        return processesModel.getRowCount()-1;

    }
    
    public String stateToString(int state) {
    	
    	switch (state) {
    		case Constants.ASKING_FOR_INPUT : return "Asking for input"; 
    		case Constants.ASKING_FOR_OUTPUT : return "Asking for output"; 
    		case Constants.ASKING_FOR_PROCESSOR : return "Asking for processor"; 
    		case Constants.HAVING_INPUT : return "Having input"; 
    		case Constants.HAVING_OUTPUT : return "Having output"; 
    		case Constants.HAVING_PROCESSOR : return "Having processor";
    		case Constants.IDLE : return "Idle";
    		case Constants.RUNNING : return "Running"; 
                case Constants.HALT : return "HALT!";
    	}
    	
    	return "Unknown state";
    	
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button button1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField modeField;
    private javax.swing.JTextField pcField;
    private javax.swing.JTextField piField;
    private javax.swing.JTextField ptrField;
    private javax.swing.JTextField siField;
    private javax.swing.JTextField spField;
    private javax.swing.JTextField tiField;
    private javax.swing.JTable userMem;
    private javax.swing.table.DefaultTableModel userMemModel;
    // End of variables declaration//GEN-END:variables
    private javax.swing.table.DefaultTableModel processesModel;
}
